package com.lwbldy.modules.system.service.impl;

import com.lwbldy.modules.system.domain.SysMenu;
import com.lwbldy.modules.system.mapper.SysMenuDao;
import com.lwbldy.modules.system.mapper.SysRoleMenuDao;
import com.lwbldy.modules.system.service.SysMenuService;
import com.lwbldy.modules.system.vo.MenuVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("sysMenuServiceImpl")
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    SysMenuDao sysMenuDao;

    @Autowired
    SysRoleMenuDao sysRoleMenuDao;


    @Override
//    @Cacheable(cacheNames="MINUTE",key = "'suihaoList'")
    public List<SysMenu> listAllSysMenu() {
        return sysMenuDao.queryList(null);
    }

    @Override
    public List<MenuVO> queryAllTreeMenu() {
        //查询所有
        List<SysMenu> rootMenu = listAllSysMenu();

        //先找到所有的一级菜单
        List<MenuVO> menuList = new ArrayList<MenuVO>();
        for(SysMenu sysMenu : rootMenu){
            if(sysMenu.getParentId() == 0){
                menuList.add(menuToVO(sysMenu));
            }
        }

        for (MenuVO menu : menuList) {
            menu.setChildren(getTreeChildren(menu.getId(),rootMenu));
        }

        return menuList;
    }

    @Override
    public List<MenuVO> queryMenuByRole(Long roleId) {
        //查询所有
        List<SysMenu> rootMenu = listAllSysMenu();

        List<Integer> menuIdList = sysRoleMenuDao.queryMenuIdList(roleId);


        //先找到所有的一级菜单
        List<MenuVO> menuList = new ArrayList<MenuVO>();
        for(SysMenu sysMenu : rootMenu){
            MenuVO vo = menuToVO(sysMenu);
            menuList.add(vo);
        }

        menuList.forEach(menuVO -> {
            menuIdList.forEach(menuId->{
                int men = Integer.parseInt(menuVO.getId().toString());
                if(men == menuId){
                    menuVO.setCheckArr("1");
                }
            });
        });

        return menuList;
    }

    @Override
    public List<MenuVO> queryMenuByUserId(Long userId) {
        List<SysMenu> rootMenu = sysMenuDao.selectMenuByUserId(userId);

        //先找到所有的一级菜单
        List<MenuVO> menuList = new ArrayList<MenuVO>();
        for(SysMenu sysMenu : rootMenu){
            if(sysMenu.getParentId() == 0){
                menuList.add(menuToVO(sysMenu));
            }
        }

        for (MenuVO menu : menuList) {
            menu.setChildren(getTreeChildren(menu.getId(),rootMenu));
        }
        return menuList;
    }

    private MenuVO menuToVO(SysMenu sysMenu){
        MenuVO treeVO = new MenuVO();
        treeVO.setId(sysMenu.getMenuId());
        treeVO.setIcon(sysMenu.getIcon());
        treeVO.setHref(sysMenu.getUrl());
        treeVO.setType(sysMenu.getType());
        treeVO.setTitle(sysMenu.getName());
        treeVO.setOpenType(sysMenu.getTager());
        treeVO.setParentId(sysMenu.getParentId());
        return treeVO;
    }


    private List<MenuVO> getTreeChildren(long id,List<SysMenu> rootMenu){
        List<MenuVO> childList = new ArrayList<MenuVO>();
        //循环所有菜单
        for(SysMenu menu:rootMenu){
            //如果是和父id相同
            if(menu.getParentId() == id){
//                MenuVO treeVO = new MenuVO();
//                treeVO.setId(menu.getMenuId());
//                treeVO.setIcon(menu.getIcon());
//                treeVO.setHref(menu.getUrl());
//                treeVO.setType(menu.getType());
//                treeVO.setTitle(menu.getName());
//                treeVO.setOpenType(menu.getTager());
//                treeVO.setParentId(menu.getParentId());
//                childList.add(treeVO);
                childList.add(menuToVO(menu));

            }
        }
        if(childList.size() == 0){
            return null;
        }
        return childList;
    }


    @Override
    public int saveSysMenu(SysMenu sysMenu) {
        return sysMenuDao.save(sysMenu);
    }

    @Override
    public SysMenu queryById(long id) {
        return sysMenuDao.queryObject(id);
    }

    @Override
    public int update(SysMenu sysMenu) {
        return sysMenuDao.update(sysMenu);
    }

    @Override
    public int remove(Long id) {
        return sysMenuDao.delete(id);
    }

    @Override
    public int removeBatch(Long[] ids) {
        return sysMenuDao.deleteBatch(ids);
    }
}
